﻿using JsonConversion;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using RTracker.Models.Common;
using RTracker.Models.ErrorLog;
using System.Web.Security;

namespace RTracker.Areas.Recruitment.Models.VacancyStatus
{
    public class MVacancyStatus
    {
        /// static variable RowSize from config settings to get list of this row size.
        public static int nRowSize = Convert.ToInt32(ConfigurationManager.AppSettings["TotalRecordsInListVacancyStatus"]);

        /// <summary>
        /// This method is used to create a VacancyStatus in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details</returns>
        public CStatus CreateVacancyStatus(CAuthenticationContext objCAuthenticationContext, ref CVacancyStatus objCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_create_vacancy_status_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCVacancyStatus.OrganizationId;
                    cmd.Parameters.Add("@vacancy_status_name", SqlDbType.NVarChar).Value = objCVacancyStatus.VacancyStatusName;
                    cmd.Parameters.Add("@vacancy_status_desc", SqlDbType.NVarChar).Value = objCVacancyStatus.VacancyStatusDesc;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// This method is used to delete a VacancyStatus in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus DeleteVacancyStatus(CAuthenticationContext objCAuthenticationContext, ref CVacancyStatus objCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_delete_vacancy_status_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    cmd.Parameters.Add("@vacancy_status_ids", SqlDbType.NVarChar).Value = objCVacancyStatus.VacancyStatusIds;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// This method is used to update the VacancyStatus's detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus UpdateVacancyStatus(CAuthenticationContext objCAuthenticationContext, ref CVacancyStatus objCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_update_vacancy_status_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@vacancy_status_id", SqlDbType.Int).Value = objCVacancyStatus.VacancyStatusId;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCVacancyStatus.OrganizationId;
                    cmd.Parameters.Add("@vacancy_status_name", SqlDbType.NVarChar).Value = objCVacancyStatus.VacancyStatusName;
                    cmd.Parameters.Add("@vacancy_status_desc", SqlDbType.NVarChar).Value = objCVacancyStatus.VacancyStatusDesc;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }

            return oCStatus;
        }

        /// <summary>
        /// This method is used to get the VacancyStatus's detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetVacancyStatusDetail(CAuthenticationContext objCAuthenticationContext, ref CVacancyStatus objCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_vacancy_status_detail_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@vacancy_status_id", SqlDbType.Int).Value = objCVacancyStatus.VacancyStatusId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;

                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        objCVacancyStatus = dt.ConvertToListFromDataTableVacancyStatus()[0];
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.DetailSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetVacancyStatusSoftLock(CAuthenticationContext objCAuthenticationContext, CVacancyStatus objCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_vacancy_status_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@vacancy_status_id", SqlDbType.Int).Value = objCVacancyStatus.VacancyStatusId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Sets the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetVacancyStatusHardLock(CAuthenticationContext objCAuthenticationContext, CVacancyStatus objCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_vacancy_status_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@vacancy_status_id", SqlDbType.Int).Value = objCVacancyStatus.VacancyStatusId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Releases the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseVacancyStatusSoftLock(CAuthenticationContext objCAuthenticationContext, CVacancyStatus objCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_vacancy_status_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@vacancy_status_id", SqlDbType.Int).Value = objCVacancyStatus.VacancyStatusId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Releases the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseVacancyStatusHardLock(CAuthenticationContext objCAuthenticationContext, CVacancyStatus objCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_vacancy_status_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@vacancy_status_id", SqlDbType.Int).Value = objCVacancyStatus.VacancyStatusId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        ///  This function is used to get complete VacancyStatus list
        /// </summary>
        /// <returns>List of type CVacancyStatus</returns>
        public GVacancyStatus GetVacancyStatusGridList(CVacancyStatus objCVacancyStatus, CAuthenticationContext objCAuthenticationContext)
        {
            GVacancyStatus objGVacancyStatus = new GVacancyStatus();
            int nTotalRecords = 0;
            int nCurrentRowSize = nRowSize;

            if (objCVacancyStatus.RowSize != 0)
            {
                nCurrentRowSize = objCVacancyStatus.RowSize;
            }
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_vacancy_status_paging_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@vacancy_status_id", SqlDbType.Int).Value = objCVacancyStatus.VacancyStatusId;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCVacancyStatus.OrganizationId;
                    cmd.Parameters.Add("@page_number", SqlDbType.Int).Value = objCVacancyStatus.PageNumber;
                    cmd.Parameters.Add("@row_size", SqlDbType.Int).Value = nCurrentRowSize;
                    cmd.Parameters.Add("@sort_on", SqlDbType.VarChar).Value = objCVacancyStatus.SortOn;
                    cmd.Parameters.Add("@sort_type", SqlDbType.VarChar).Value = objCVacancyStatus.SortType;
                    cmd.Parameters.Add("@search", SqlDbType.VarChar).Value = objCVacancyStatus.Search;
                    cmd.Parameters.Add("@searchon", SqlDbType.VarChar).Value = objCVacancyStatus.SearchOn;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    objGVacancyStatus.Paging = new CPaging();
                    if (rowsAffected > 0)
                    {
                        objGVacancyStatus.lstVacancyStatus = dt.ConvertToListFromDataTableVacancyStatus();
                        objGVacancyStatus.lstVacancyStatus[0].PageNumber = int.Parse(cmd.Parameters["@idOut"].Value.ToString());
                        objGVacancyStatus.Paging.PageNumber = objGVacancyStatus.lstVacancyStatus[0].PageNumber;
                    }
                    else
                    {
                        objGVacancyStatus.lstVacancyStatus = null;
                    }


                    nTotalRecords = int.Parse(cmd.Parameters["@error"].Value.ToString());

                    objGVacancyStatus.Status = new CStatus();

                    if (nTotalRecords < 0)
                    {
                        objGVacancyStatus.Status.Success = false;
                        objGVacancyStatus.Status.StatusDetails = nTotalRecords.DbError();
                    }
                    else
                    {
                        objGVacancyStatus.Paging.TotalRecordsCount = nTotalRecords;
                        objGVacancyStatus.Paging.PageSize = nCurrentRowSize;

                        objGVacancyStatus.Status.Success = true;
                        objGVacancyStatus.Status.StatusDetails = "success";
                    }



                }
                catch (Exception ex)
                {
                    objGVacancyStatus.Status = new CStatus();
                    objGVacancyStatus.Status.Success = false;
                    objGVacancyStatus.Status.StatusDetails = ex.Message;
                }
            }
            return objGVacancyStatus;
        }

        /// /// <summary>
        ///  This function is used to get complete VacancyStatus list working under a same organization.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about VacancyStatus's authentication context.</param>
        /// <param name="objCVacancyStatus">This object contains details of the VacancyStatus.</param>
        /// <param name="lstCVacancyStatus">"out" the list of VacancyStatuss</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetVacancyStatusList(CAuthenticationContext objCAuthenticationContext, int organizationId, out List<CVacancyStatus> lstCVacancyStatus)
        {
            CStatus oCStatus = new CStatus();
            lstCVacancyStatus = new List<CVacancyStatus>();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_get_vacancy_status_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = organizationId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    //cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    //cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        lstCVacancyStatus.AddRange(dt.ConvertToListFromDataTableVacancyStatus());
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }


    }
}